'use strict';

const db = uniCloud.database()
const $ = db.command.aggregate

exports.main = async (event, context) => {
	//event为客户端上传的参数
	const { name, page, pageSize, userId } = event
	
	console.log(userId)
	
	let  matchObj = {}
	
	if(name !== '全部') {
		matchObj = {
			classify: name
		}
	}
	const userInfo = await db.collection('user').doc(userId).get()
	const article_likes_ids = userInfo.data[0].article_likes_ids
	 
	const list = await db.collection('article')
	.aggregate()
	.addFields({
		is_like: $.in(['$_id', article_likes_ids])
	})
	.match(matchObj)
	.project({
		content: 0
	})
	.skip(pageSize * (page - 1))
	.limit(pageSize)
	.end()
	
	// 单纯过滤内容
	// const list = await db.collection('article')
	// .field({
	// 	content: false
	// })
	// .get()
	
	//返回数据给客户端
	return {
		code: 200,
		msg: '数据请求成功',
		data: list.data
	}
};
